package com.example.studytools

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.graphics.RectF
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.widget.FrameLayout
import android.widget.RelativeLayout
import com.blankj.utilcode.util.ScreenUtils
import com.blankj.utilcode.util.SizeUtils
/**
 * 尺子布局
 */
class RulerLayout@JvmOverloads constructor(
  private val context: Context, attrs: AttributeSet? = null
) : RelativeLayout(context, attrs) {

    private lateinit var addLengthBtn:View
    private lateinit var rulerView:RulerView
    private lateinit var transformLayout: TransformLayout
    private lateinit var rotateView: View
    private lateinit var deleteIv:View

    private var lastX:Int = 0
    private var dX:Int = 0

    init {
        initView()
    }

    @SuppressLint("ClickableViewAccessibility")
    private fun initView() {
        LayoutInflater.from(context).inflate(R.layout.ruler_layout, this)
        addLengthBtn = findViewById(R.id.add_length)
        rulerView = findViewById(R.id.ruler_view)
        transformLayout = findViewById(R.id.transform_layout)
        transformLayout.translationX= ScreenUtils.getScreenWidth()/2f-SizeUtils.dp2px(560f)/2f
        transformLayout.translationY= ScreenUtils.getScreenHeight()/2f-SizeUtils.dp2px(50f)/2f
        deleteIv = findViewById(R.id.close_view)
        deleteIv.setOnClickListener {
            (this@RulerLayout.parent as FrameLayout).removeView(this@RulerLayout)
        }
        rotateView = findViewById(R.id.rotate_view)
        rotateView.setOnTouchListener { v, event ->
            val offsetX: Float = (rotateView.left-transformLayout.scrollX).toFloat()
            val offsetY: Float = (rotateView.top-transformLayout.scrollY).toFloat()
            event.offsetLocation(offsetX, offsetY)

            transformLayout.pivotX = transformLayout.left+transformLayout.width/2f
            transformLayout.pivotY = transformLayout.top+transformLayout.height/2f
            transformLayout.rotateLayout(event)
            true
        }

        //这里要设置背景色，不然上边距会被裁掉
        setBackgroundColor(Color.TRANSPARENT)
        addLengthBtn.setOnTouchListener { v, event ->
            when (event.actionMasked) {
                MotionEvent.ACTION_DOWN -> {
                    lastX = event.rawX.toInt()
                }
                MotionEvent.ACTION_MOVE -> {
                    //1680和2500

                    //获取当前实时点信息
                    val rawX = event.rawX.toInt()
                    //变化量
                    dX = rawX - lastX

                    val params = transformLayout.layoutParams as LayoutParams
                    //这里指定下宽高，否则如果布局文件指定了match_parent/wrap_content，会导致自动伸缩宽高
                    params.width = transformLayout.measuredWidth + dX
                    params.height = transformLayout.measuredHeight
                    transformLayout.layoutParams = params
                    //更新最后屏幕点信息
                    lastX = rawX
                }
                MotionEvent.ACTION_UP -> {}
                else -> {}
            }
            return@setOnTouchListener true
        }
    }

}